<template>
  <el-card>
    <div class="mb-20px">
      <h4>隐私政策</h4>
    </div>
    <div>最近更新时间：{{ detail.updateTime ? formatDate(detail.updateTime as any) : '-' }}</div>
    <div class="m-y-20px">
      <el-button type="primary" :loading="formLoading" @click="submitForm">保存</el-button>
    </div>
    <div>
      <Editor v-model="detail.content" height="500px" />
    </div>
  </el-card>
</template>
<script setup lang="ts">
import { PrivacyApi, Privacy } from '@/api/biz/privacy'
import { formatDate } from '@/utils/formatTime'

const { t } = useI18n() // 国际化
const message = useMessage()

const detail = ref<Privacy>({
  id: 1,
  content: '',
  updateTime: undefined
})
const formLoading = ref(false)

const submitForm = async () => {
  // 提交请求
  formLoading.value = true
  try {
    const data = detail.value as unknown as Privacy
    await PrivacyApi.updatePrivacy({ ...data, updateTime: Date.now().toString() })
    message.success(t('common.updateSuccess'))
    PrivacyApi.getPrivacy(1).then((data) => {
      detail.value = data
    })
  } finally {
    formLoading.value = false
  }
}

PrivacyApi.getPrivacy(1).then((data) => {
  detail.value = data
})
</script>
